package com.qf.servlet;


import com.qf.pojo.User;
import com.qf.service.UserService;
import com.qf.service.UserServiceImpl;
import com.qf.utils.Base64Util;
import com.qf.utils.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@WebServlet("/userservlet")
public class UserServlet extends BaseServlet{

   //  注册
    public String userRegist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        //获取前端数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //校验
        if (StringUtil.isEmpty(username)){
            request.setAttribute("msg","用户名不能为空");
            return "/regist.jsp";
        }
        if (StringUtil.isEmpty(password)){
            request.setAttribute("msg","密码不能为空");
            return "/regist.jsp";
        }
        //调用业务逻辑
        try {
            UserService userService = new UserServiceImpl();
            //密码加密
            password = Base64Util.encode(password);
            User user = new User(username,password,1);
            userService.insert(user);
            return "redirect:login.jsp";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("error","注册失败"+e.getMessage());
            return "/error.jsp";
        }

    }
    /**
     * 登录
     */
    public String userLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取前端数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //登录用户输入校验
        if (StringUtil.isEmpty(username)){
            request.setAttribute("msg","用户名不能为空");
            return "/login.jsp";
        }
        if (StringUtil.isEmpty(password)){
            request.setAttribute("msg","密码不能为空");
            return "/login.jsp";
        }
        try {
            //调用业务逻辑
            UserService userService = new UserServiceImpl();
            User user = userService.login(username,password);
            request.getSession().setAttribute("user",user);
            return "profileservlet?method=listProfile";
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("error","登录失败"+e.getMessage());
            return "/error.jsp";
        }

    }
    /**
     * 判断用户是否存在
     */
    public String userCheck(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        response.setContentType("text/html;charset=utf-8");
        //获取前端数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //如果想用页面的的效果下面的注释恢复效果,将调用业务逻辑注释
        /*//登录用户输入校验
        if (StringUtils.isEmpty(username)){
            response.getWriter().write("1");
            return null;
        }
        if (StringUtils.isEmpty(password)){
            response.getWriter().write("2");
            return null;
        }*/
        //调用业务逻辑
        UserService userService = new UserServiceImpl();
        boolean flag = userService.userCheckName(username);
        if (flag) {
            response.getWriter().write("0");
            return null;
        }
        response.getWriter().write("3");
        return null;
    }
    /**
     * 退出登录
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     */
    public String loginOut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        HttpSession session = request.getSession();
        session.removeAttribute("user");
        session.invalidate();
        return "redirect:/login.jsp";
    }
}
